<template>
  <h2>sum：{{ sum }}</h2>
  <button type="" @click="sum++">点击+1</button>
  <hr />
  <h2>msg：{{ msg }}</h2>
  <button type="" @click="msg += '！'">点击+！</button>
  <hr />
  <h2>{{ `姓名：${person.name}  年龄：${person.age}` }}</h2>
  <h2>薪水：{{ person.job.j1.salary }}K</h2>
  <button type="" @click="person.name += '~'">修改姓名</button>
  <button type="" @click="person.age++">增长年龄</button>
  <button type="" @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
import { ref, watch, reactive } from "vue";
export default {
  name: "App",
  setup() {
    let sum = ref(0);
    let msg = ref("你好啊");
    let person = ref({
      name: "张三",
      age: 18,
      job: {
        j1: {
          salary: 15,
        },
      },
    });

    // watch(sum, (newVal, oldVal) => {
    //   console.log("sum变化了 :>> ", newVal, oldVal);
    // });

    // watch 监听ref定义的对象类型的数据时，有两种写法
    // 1:person.value，watch监听ref对象里面的对象是，不是深度的，所以加上.value，使其监听Proxy对象
    // Proxy对象是借助了reactive来定义的，默认开启的深度监听
    // 2:加上deep:true,开启深度监听即可
    console.log('person :>> ', person);
    watch(person,(newVal, oldVal) => {
      console.log("person变化了 :>> ", newVal, oldVal);
    },{deep:true})

    return {
      sum,
      msg,
      person,
    };
  },
};
</script>
